using DevExpress.Data;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Menu;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using Common.Class;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace Common.Base
{
	public class XucBaseBasic : XucBase
	{
		private IContainer components = null;
		protected XucToolBar ucToolBar;
		protected GridControl gcList;
		protected AdvBandedGridView gbList;
		protected GridBand gridBand1;
		protected Common.Class.RowClickEventArgs MRowClickEventArgs = new Common.Class.RowClickEventArgs();
		protected bool _search;
		public event ButtonClickEventHander CloseClick;
		public bool IsSearch
		{
			get
			{
				return this._search;
			}
			set
			{
				this._search = value;
			}
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			this.ucToolBar = new XucToolBar();
			this.gbList = new AdvBandedGridView();
			this.gridBand1 = new GridBand();
			this.gcList = new GridControl();
			((ISupportInitialize)this.gbList).BeginInit();
			((ISupportInitialize)this.gcList).BeginInit();
			base.SuspendLayout();
			this.ucToolBar.ButtonAdd = BarItemVisibility.Always;
			this.ucToolBar.ButtonCancel = BarItemVisibility.Never;
			this.ucToolBar.ButtonChange = BarItemVisibility.Never;
			this.ucToolBar.ButtonDelete = BarItemVisibility.Always;
			this.ucToolBar.ButtonEdit = BarItemVisibility.Always;
			this.ucToolBar.ButtonExport = BarItemVisibility.Always;
			this.ucToolBar.ButtonOption = BarItemVisibility.Never;
			this.ucToolBar.ButtonPrint = BarItemVisibility.Always;
			this.ucToolBar.ButtonSave = BarItemVisibility.Never;
			this.ucToolBar.ButtonSaveNew = BarItemVisibility.Never;
			this.ucToolBar.Dock = DockStyle.Top;
			this.ucToolBar.IsClose = CloseOrNew.None;
			this.ucToolBar.Location = new Point(0, 0);
			this.ucToolBar.Name = "ucToolBar";
			this.ucToolBar.Size = new Size(761, 43);
			this.ucToolBar.Status = Actions.Add;
			this.ucToolBar.TabIndex = 0;
			this.ucToolBar.Title = "";
			this.gbList.Bands.AddRange(new GridBand[]
			{
				this.gridBand1
			});
			this.gbList.GridControl = this.gcList;
			this.gbList.GroupSummary.AddRange(new GridSummaryItem[]
			{
				new GridGroupSummaryItem(SummaryItemType.None, "", null, "")
			});
			this.gbList.IndicatorWidth = 45;
			this.gbList.Name = "gbList";
			this.gbList.OptionsPrint.ExpandAllDetails = true;
			this.gbList.OptionsPrint.PrintDetails = true;
			this.gbList.OptionsPrint.PrintFilterInfo = true;
			this.gbList.OptionsPrint.PrintPreview = true;
			this.gbList.OptionsSelection.InvertSelection = true;
			this.gbList.OptionsSelection.MultiSelect = true;
			this.gbList.OptionsView.ColumnAutoWidth = true;
			this.gbList.OptionsView.HeaderFilterButtonShowMode = FilterButtonShowMode.Button;
			this.gbList.OptionsView.ShowAutoFilterRow = true;
			this.gbList.OptionsView.ShowBands = false;
			this.gbList.OptionsView.ShowDetailButtons = false;
			this.gbList.OptionsView.ShowFooter = true;
			this.gbList.OptionsView.ShowGroupPanel = false;
			this.gbList.ShowButtonMode = ShowButtonModeEnum.ShowAlways;
			this.gbList.CustomDrawRowIndicator += new RowIndicatorCustomDrawEventHandler(this.GbListCustomDrawRowIndicator);
			this.gbList.KeyDown += new KeyEventHandler(this.GbListKeyDown);
			this.gbList.Click += new EventHandler(this.GbListClick);
			this.gbList.Layout += new EventHandler(this.GbListLayout);
			this.gbList.MouseDown += new MouseEventHandler(this.GbListMouseDown);
			this.gbList.DoubleClick += new EventHandler(this.GbListDoubleClick);
			this.gbList.RowCellClick += new RowCellClickEventHandler(this.GbListRowCellClick);
			this.gridBand1.Caption = "DANH MỤC";
			this.gridBand1.Name = "gridBand1";
			this.gridBand1.OptionsBand.ShowCaption = false;
			this.gridBand1.ToolTip = "DANH MỤC";
			this.gcList.Dock = DockStyle.Fill;
			this.gcList.Location = new Point(0, 43);
			this.gcList.LookAndFeel.SkinName = "Office 2007 Black";
			this.gcList.MainView = this.gbList;
			this.gcList.Name = "gcList";
			this.gcList.Size = new Size(761, 454);
			this.gcList.TabIndex = 10;
			this.gcList.ViewCollection.AddRange(new BaseView[]
			{
				this.gbList
			});
			base.AutoScaleDimensions = new SizeF(6f, 13f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.Controls.Add(this.gcList);
			base.Controls.Add(this.ucToolBar);
			this.DoubleBuffered = true;
			base.Name = "XucBaseBasic";
			base.Size = new Size(761, 497);
			((ISupportInitialize)this.gbList).EndInit();
			((ISupportInitialize)this.gcList).EndInit();
			base.ResumeLayout(false);
		}
		public XucBaseBasic()
		{
			this.InitializeComponent();
			this._search = false;
			this.DisableMenu(true);
		}
		protected override bool ProcessDialogKey(Keys keyData)
		{
			bool result;
			if (keyData <= Keys.F5)
			{
				if (keyData == Keys.Escape)
				{
					this.RaiseCloseClickEventHander();
					result = true;
					return result;
				}
				if (keyData == Keys.F1)
				{
					XucBaseBasic.UcToolBarHelpClick(this.ucToolBar);
					result = true;
					return result;
				}
				if (keyData == Keys.F5)
				{
					this.ReLoad();
					result = true;
					return result;
				}
			}
			else
			{
				if (keyData <= (Keys)131156)
				{
					switch (keyData)
					{
					case (Keys)131141:
						this.UcToolBarExport(this.ucToolBar);
						result = true;
						return result;
					case (Keys)131142:
						this.UcToolBarSearchClick(this.ucToolBar);
						result = true;
						return result;
					default:
						switch (keyData)
						{
						case (Keys)131150:
							this.UcToolBarAddClick(this.ucToolBar);
							result = true;
							return result;
						case (Keys)131151:
							XucBaseBasic.UcToolBarOptionClick(this.ucToolBar);
							result = true;
							return result;
						case (Keys)131152:
							this.UcToolBarPrintClick(this.ucToolBar);
							result = true;
							return result;
						case (Keys)131156:
							this.UcToolBarAddClick(this.ucToolBar);
							result = true;
							return result;
						}
						break;
					}
				}
				else
				{
					if (keyData == (Keys)262212)
					{
						MessageBox.Show("You are stupid");
						result = true;
						return result;
					}
					if (keyData == (Keys)262232)
					{
						this.RaiseCloseClickEventHander();
						result = true;
						return result;
					}
				}
			}
			result = false;
			return result;
		}
		private void RaiseCloseClickEventHander()
		{
			if (this.CloseClick != null)
			{
				this.CloseClick(this);
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			this.Init();
			base.OnLoad(e);
		}
		private void Init()
		{
			this.ucToolBar.AddClick += new ButtonClickEventHander(this.UcToolBarAddClick);
			this.ucToolBar.EditClick += new ButtonClickEventHander(this.UcToolBarEditClick);
			this.ucToolBar.DeleteClick += new ButtonClickEventHander(this.UcToolBarDeleteClick);
			this.ucToolBar.SearchClick += new ButtonClickEventHander(this.UcToolBarSearchClick);
			this.ucToolBar.PrintClick += new ButtonClickEventHander(this.UcToolBarPrintClick);
			this.ucToolBar.RefreshClick += new ButtonClickEventHander(this.UcToolBarRefreshClick);
			this.ucToolBar.OptionClick += new ButtonClickEventHander(XucBaseBasic.UcToolBarOptionClick);
			this.ucToolBar.ExportClick += new ButtonClickEventHander(this.UcToolBarExport);
			this.ucToolBar.HelpClick += new ButtonClickEventHander(XucBaseBasic.UcToolBarHelpClick);
			this.ucToolBar.CloseClick += new ButtonClickEventHander(this.UcToolBarCloseClick);
			this.ucToolBar.ImportClick += new ButtonClickEventHander(this.UcToolBarImportClick);
			this.ucToolBar.PermisionClick += new ButtonClickEventHander(this.UcToolBarPermisionClick);
			this.ucToolBar.ConstructClick += new ButtonClickEventHander(this.UcToolBarConstructClick);
			this.ucToolBar.CopyClick += new ButtonClickEventHander(this.UcToolBarCopyClick);
			this.ucToolBar.ClearClick += new ButtonClickEventHander(this.UcToolBarClearClick);
			this.ReLoad();
			this.ucToolBar.bm.SetPopupContextMenu(this.gcList, this.ucToolBar.pm);
		}
		private void UcToolBarClearClick(object sender)
		{
			if (this.gbList.RowCount != 0)
			{
				this.gbList.SelectAll();
				this.Delete();
				this.SetMenu(this.MRowClickEventArgs);
			}
		}
		public virtual void Mirror()
		{
		}
		private void UcToolBarCopyClick(object sender)
		{
			this.gbList.CopyToClipboard();
		}
		public void Construct()
		{
		}
		private void UcToolBarConstructClick(object sender)
		{
			this.Construct();
		}
		public virtual void Permision()
		{
		}
		private void UcToolBarPermisionClick(object sender)
		{
			this.Permision();
		}
		public virtual void Import()
		{
		}
		private void UcToolBarImportClick(object sender)
		{
			this.Import();
		}
		private void UcToolBarCloseClick(object sender)
		{
			this.RaiseCloseClickEventHander();
		}
		private static void UcToolBarHelpClick(object sender)
		{
			Process.Start("http://www.perfect.com.vn/huong-dan-su-dung-phan-mem-quan-ly-doanh-nghiep-perfect-erp.htm");
		}
		private void UcToolBarExport(object sender)
		{
			this.Export(this.gbList);
		}
		private static void UcToolBarOptionClick(object sender)
		{
			XtraMessageBox.Show("Tính năng này không được hỗ trợ!");
		}
		private void UcToolBarRefreshClick(object sender)
		{
			this.ReLoad();
		}
		private void UcToolBarPrintClick(object sender)
		{
			this.Print();
		}
		private void UcToolBarSearchClick(object sender)
		{
			this.Search();
		}
		private void UcToolBarDeleteClick(object sender)
		{
			if (this.gbList.RowCount != 0)
			{
				this.Delete();
				this.SetMenu(this.MRowClickEventArgs);
			}
		}
		private void UcToolBarEditClick(object sender)
		{
			if (this.gbList.RowCount != 0)
			{
				this.Change();
			}
		}
		private void UcToolBarAddClick(object sender)
		{
			this.Add();
		}
		public virtual void Delete()
		{
		}
		protected virtual void Add()
		{
		}
		public virtual void Change()
		{
		}
		protected void Search()
		{
			XtraMessageBox.Show("Tính năng này không được hỗ trợ!");
		}
		protected virtual void Print()
		{
			this.gcList.ShowPrintPreview();
		}
		public virtual void ReLoad()
		{
		}
		public object GetCellValue(Common.Class.RowClickEventArgs e)
		{
			AdvBandedGridView advBandedGridView = this.gbList;
			GridColumn column = new GridColumn();
			if (e.ColumnIndex != -1)
			{
				column = advBandedGridView.Columns[e.ColumnIndex];
			}
			else
			{
				column = advBandedGridView.Columns[e.FieldName];
			}
			return advBandedGridView.GetRowCellValue(e.RowIndex, column);
		}
		public object GetCellValue(int rowIndex, string fieldName)
		{
			return this.GetCellValue(new Common.Class.RowClickEventArgs(rowIndex, fieldName));
		}
		protected void GbListClick(object sender, EventArgs e)
		{
			if (sender is AdvBandedGridView)
			{
				AdvBandedGridView advBandedGridView = (AdvBandedGridView)sender;
				Common.Class.RowClickEventArgs rowClickEventArgs = new Common.Class.RowClickEventArgs((advBandedGridView == null) ? -1 : advBandedGridView.FocusedRowHandle, (advBandedGridView.FocusedColumn == null) ? -1 : advBandedGridView.FocusedColumn.ColumnHandle, (advBandedGridView.FocusedColumn == null) ? "" : advBandedGridView.FocusedColumn.FieldName);
				this.MRowClickEventArgs = rowClickEventArgs;
				this.ItemSelectd(rowClickEventArgs);
			}
			else
			{
				XtraMessageBox.Show("Object is not AdvBandedGridView");
			}
		}
		protected void ItemSelectd(Common.Class.RowClickEventArgs ex)
		{
		}
		protected void GbListDoubleClick(object sender, EventArgs e)
		{
			if (sender is AdvBandedGridView)
			{
				AdvBandedGridView advBandedGridView = (AdvBandedGridView)sender;
				Common.Class.RowClickEventArgs rowClickEventArgs = new Common.Class.RowClickEventArgs(advBandedGridView.FocusedRowHandle, (advBandedGridView.FocusedColumn == null) ? -1 : advBandedGridView.FocusedColumn.ColumnHandle, (advBandedGridView.FocusedColumn == null) ? "" : advBandedGridView.FocusedColumn.FieldName);
				this.MRowClickEventArgs = rowClickEventArgs;
				if (!this._search)
				{
					if (this.gbList.RowCount != 0)
					{
						this.Change();
					}
				}
				else
				{
					this.SetSearch(rowClickEventArgs);
				}
			}
			else
			{
				XtraMessageBox.Show("Object is not AdvBandedGridView");
			}
		}
		public virtual void SetSearch(Common.Class.RowClickEventArgs e)
		{
		}
		private void UcListListKeyDown(object sender, KeyEventArgs key)
		{
			if (key.KeyCode == Keys.F2)
			{
				if (this.gbList.RowCount != 0)
				{
					this.Change();
				}
			}
			else
			{
				if (key.KeyCode == Keys.Control | key.KeyCode == Keys.E)
				{
					if (this.gbList.RowCount != 0)
					{
						this.Change();
					}
				}
				else
				{
					if (key.KeyCode == Keys.Return)
					{
						if (this.gbList.RowCount != 0)
						{
							this.Change();
						}
					}
					else
					{
						if (key.KeyCode == Keys.F5)
						{
							this.ReLoad();
						}
						else
						{
							if (key.KeyCode == Keys.Control | key.KeyCode == Keys.N)
							{
								this.Add();
							}
							else
							{
								if (key.KeyCode == Keys.Control | key.KeyCode == Keys.T)
								{
									this.Add();
								}
							}
						}
					}
				}
			}
		}
		protected void GbListKeyDown(object sender, KeyEventArgs e)
		{
			if (sender is AdvBandedGridView)
			{
				AdvBandedGridView advBandedGridView = (AdvBandedGridView)sender;
				Common.Class.RowClickEventArgs mRowClickEventArgs = new Common.Class.RowClickEventArgs(advBandedGridView.FocusedRowHandle, (advBandedGridView.FocusedColumn == null) ? -1 : advBandedGridView.FocusedColumn.ColumnHandle, (advBandedGridView.FocusedColumn == null) ? "" : advBandedGridView.FocusedColumn.FieldName);
				this.UcListListKeyDown(sender, e);
				this.MRowClickEventArgs = mRowClickEventArgs;
				if (e.KeyCode == Keys.Delete)
				{
					if (this.gbList.RowCount != 0)
					{
						this.Delete();
						this.SetMenu(this.MRowClickEventArgs);
					}
				}
			}
			else
			{
				XtraMessageBox.Show("Object is not AdvBandedGridView");
			}
		}
		protected override bool ExportPermision()
		{
			return true;
		}
		protected virtual void GbListMouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left)
			{
				this.DoShowMenu(this.gbList.CalcHitInfo(new Point(e.X, e.Y)));
			}
			AdvBandedGridView advBandedGridView = (AdvBandedGridView)sender;
			Common.Class.RowClickEventArgs rowClickEventArgs = new Common.Class.RowClickEventArgs((advBandedGridView == null) ? -1 : advBandedGridView.FocusedRowHandle, (advBandedGridView.FocusedColumn == null) ? -1 : advBandedGridView.FocusedColumn.ColumnHandle, (advBandedGridView.FocusedColumn == null) ? "" : advBandedGridView.FocusedColumn.FieldName);
			this.MRowClickEventArgs = rowClickEventArgs;
			this.SetMenu(rowClickEventArgs);
		}
		protected virtual void SetMenu(Common.Class.RowClickEventArgs e)
		{
		}
		protected void DoShowMenu(GridHitInfo hi)
		{
			if (hi.HitTest == GridHitTest.ColumnButton)
			{
				GridViewMenu gridViewMenu = new GridViewColumnButtonMenu(this.gbList);
				gridViewMenu.Init(hi);
				gridViewMenu.Show(hi.HitPoint);
			}
		}
		public void DisableMenu(bool disable)
		{
			this.ucToolBar.bbiEdit.Enabled = !disable;
			this.ucToolBar.bbiDelete.Enabled = !disable;
			this.ucToolBar.bbiDeleteAll.Enabled = !disable;
			this.ucToolBar.bbiPermision.Enabled = !disable;
			this.ucToolBar.bbiCopy.Enabled = !disable;
			this.ucToolBar.bbiPrint.Enabled = !disable;
			this.ucToolBar.bbiExport.Enabled = !disable;
		}
		private void GbListCustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
		{
			int num = e.RowHandle;
			if (num >= 0)
			{
				num++;
				e.Info.DisplayText = num.ToString();
			}
		}
		public virtual void SaveLayout(string name)
		{
			if (!Directory.Exists(Application.StartupPath + "\\Layout"))
			{
				Directory.CreateDirectory(Application.StartupPath + "\\Layout");
			}
			try
			{
				this.gbList.SaveLayoutToXml(Application.StartupPath + "\\Layout\\" + name + ".xml");
			}
			catch (Exception ex)
			{
				XtraMessageBox.Show("Lỗi:\n\t" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}
		public virtual void SaveLayout()
		{
			this.SaveLayout(base.Name);
		}
		public void ReadLayout(string name)
		{
			FileInfo fileInfo = new FileInfo(Application.StartupPath + "\\Layout\\" + name + ".xml");
			if (fileInfo.Exists)
			{
				try
				{
					this.gbList.RestoreLayoutFromXml(fileInfo.FullName);
				}
				catch (Exception ex)
				{
					throw new Exception(ex.Message);
				}
			}
		}
		protected void GbListLayout(object sender, EventArgs e)
		{
			this.SaveLayout();
		}
		protected void GbListRowCellClick(object sender, RowCellClickEventArgs e)
		{
			Common.Class.RowClickEventArgs rowClickEventArgs = new Common.Class.RowClickEventArgs(e.RowHandle, e.Column.ColumnHandle);
			this.MRowClickEventArgs = rowClickEventArgs;
			this.ItemSelectd(rowClickEventArgs);
			this.SetMenu(rowClickEventArgs);
			this.SetSearch(rowClickEventArgs);
		}
	}
}
